class BaseModel {
 
  public _id: string = ''

  // 根据数据设置对象并获取
  public static creatModelWithJson(json: object) {
    const model = new this()
    model.setDataWithJson(json)
    return model
  }

  /**
   * JSON转模型
   * @param json json数据
   */
  setDataWithJson(json: {}) {
    Object.assign(this, json)
  }

  /**
   * 通过键值获取对象
   * @param keys 键值字符串(以空格做区分)
   */
  getJson(keys: string): object {
    const arr = keys.split(' ')
    const obj = {}
    arr.forEach((key: string) => {
      (obj as any)[key] = (this as any)[key]
    })
    return obj
  }

  /**
   * 数组转模型
   * @param arr 数组
   */
  static modelsWithArr<T extends BaseModel>(arr: object[]): Array<T> {
    return arr.map((json: object) => {
      const model = this.creatModelWithJson(json) as T
      return model
    })
  }
}

export default BaseModel